package com.wanxi.servlet.suggest;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.SuggestModel;
import com.wanxi.service.Impl.SuggestServiceImpl;
import com.wanxi.service.SuggestService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/5/5 20:12
 */
@WebServlet("/back/FindAllSuggest")
public class FindAllSuggestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2.获取前端数据
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String message = req.getParameter("message");
        String enable = req.getParameter("enable");
        String remark = req.getParameter("remark");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        //3.封装model
        SuggestModel suggestModel= SuggestModel.builder()
                .name(Tool.nullToStr(name))
                .phone(Tool.nullToStr(phone))
                .email(Tool.nullToStr(email))
                .message(Tool.nullToStr(message))
                .enable(Tool.nullToInt(enable))
                .remark(Tool.nullToStr(remark))
                .page(Tool.nullToPage(page))
                .limit(Tool.nullToLimit(limit))
                .build();
        //4.调用service层
        SuggestService suggestService = new SuggestServiceImpl();
        ResultDto resultDto = suggestService.findAll(suggestModel);
        //5.返回前台数据
        resp.getWriter().println(JSONObject.toJSONString(resultDto));
    }
}
